package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotRescueRecord;
import org.dromara.iot.domain.bo.IotRescueRecordBo;
import org.dromara.iot.domain.vo.IotRescueRecordVo;
import org.dromara.iot.enums.TiringStateEnum;
import org.dromara.iot.mapper.IotRescueRecordMapper;
import org.dromara.iot.service.IIotRescueRecordService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 救援记录Service业务层处理
 *
 * @author lsl
 * @date 2024-06-23
 */
@RequiredArgsConstructor
@Service
public class IotRescueRecordServiceImpl implements IIotRescueRecordService {

    private final IotRescueRecordMapper baseMapper;

    /**
     * 查询救援记录
     */
    @Override
    public IotRescueRecordVo queryById(String recordId) {
        return baseMapper.selectVoById(recordId);
    }

    /**
     * 查询救援记录列表
     */
    @Override
    public TableDataInfo<IotRescueRecordVo> queryPageList(IotRescueRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotRescueRecord> lqw = buildQueryWrapper(bo);
        Page<IotRescueRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询救援记录列表
     */
    @Override
    public List<IotRescueRecordVo> queryList(IotRescueRecordBo bo) {
        LambdaQueryWrapper<IotRescueRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotRescueRecord> buildQueryWrapper(IotRescueRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotRescueRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTiringId() != null, IotRescueRecord::getTiringId, bo.getTiringId());
        lqw.eq(bo.getUserId() != null, IotRescueRecord::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTiringState()), IotRescueRecord::getTiringState, bo.getTiringState());
        return lqw;
    }

    /**
     * 新增救援记录
     */
    @Override
    public Boolean insertByBo(IotRescueRecordBo bo) {
        IotRescueRecord add = MapstructUtils.convert(bo, IotRescueRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改救援记录
     */
    @Override
    public Boolean updateByBo(IotRescueRecordBo bo) {
        IotRescueRecord update = MapstructUtils.convert(bo, IotRescueRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotRescueRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除救援记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据困人ID查询救援记录
     *
     * @param tiringId 困人ID
     * @return 救援记录列表
     */
    @Override
    public List<IotRescueRecordVo> queryListByTiringId(Long tiringId) {
        LambdaQueryWrapper<IotRescueRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(IotRescueRecord::getTiringId, tiringId);
        lqw.ne(IotRescueRecord::getTiringState, TiringStateEnum.REPORT.getCode());
        lqw.ne(IotRescueRecord::getTiringState, TiringStateEnum.RESCUE.getCode());
        return baseMapper.selectVoList(lqw);
    }
}
